package controllers;

import db.VehicleDAO;
import db.VehicleTypeDAO;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import java.util.HashMap;
import java.util.ArrayList;
import java.sql.Time;

@ManagedBean
@SessionScoped
public class Vehicle {

    private int id;
    private String name;
    private int vehicleTypeID;
    private String vehicleTypeName;
    private boolean brokenDown;
    private static Vehicle editableVehicle;
    private int dateCounter = 0;
    private VehicleType vehicleType;
    HashMap<Integer,ArrayList<Time>> timeUsed = new HashMap<Integer,ArrayList<Time>>();

    //<editor-fold desc="Constructors">
    public Vehicle() {
    }

    public Vehicle(int id, String name, int vehicleTypeID, String vehicleTypeName, boolean brokenDown) {
        this.id = id;
        this.name = name;
        this.vehicleTypeID = vehicleTypeID;
        this.vehicleTypeName = vehicleTypeName;
        this.brokenDown = brokenDown;

        this.vehicleType = VehicleTypeDAO.getVehicleTypeByID(vehicleTypeID);
    }
    //</editor-fold>

    //<editor-fold desc="Getters and Setters">
    public int getId() {
        return id;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setBrokenDown(boolean brokenDown) {
        this.brokenDown = brokenDown;
    }

    public boolean isBrokenDown() {
        return brokenDown;
    }

    public String getVehicleTypeName() {
        return vehicleTypeName;
    }

    public int getVehicleTypeID() {
        return vehicleTypeID;
    }

    public void setVehicleTypeID(int vehicleTypeID) {
        this.vehicleTypeID = vehicleTypeID;
    }

    public HashMap<Integer, ArrayList<Time>> getTimeUsed() {
        return timeUsed;
    }

    public void addTimeUsed(ArrayList<Time> times) {
        timeUsed.put(dateCounter++, times);
    }

    public VehicleType getVehicleType() {
        return vehicleType;
    }

    public void setVehicleType(VehicleType vehicleType) {
        this.vehicleType = vehicleType;
    }
    //</editor-fold>

    //<editor-fold desc="Editable Vehicle">
    public void setEditableVehicle(Vehicle vehicle) {
        editableVehicle = vehicle;
    }

    //Non-static method for JSF access
    public Vehicle getEditableVehicle() {
        return editableVehicle;
    }

    //Static method for Java access
    public static Vehicle getEditableVehicleStatic() {
        return editableVehicle;
    }

    public String editVehicle() {
        FacesContext fc = FacesContext.getCurrentInstance();
        setEditableVehicle((Vehicle) fc.getExternalContext().getRequestMap().get("vehicle"));
        return "editVehicle";
    }
    //</editor-fold>

    //<editor-fold desc="CRUD Methods">
    public String addVehicle() {
        this.brokenDown = false;
        if (VehicleDAO.addVehicle(this)) {
            return "vehicleList";
        } else {
            return "vehicleAddFailure";
        }
    }

    public String deleteVehicle() {
        if (VehicleDAO.deleteVehicle(this)) {
            return "vehicleList";
        } else {
            return "vehicleDeleteFailure";
        }
    }

    public List<Vehicle> getVehicles() {
        return VehicleDAO.getVehicles();
    }

    public String saveVehicle() {
        if (VehicleDAO.saveVehicle(this)) {
            editableVehicle = null;
            return "vehicleList";
        } else {
            return "VehicleSaveFailure";
        }
    }
    //</editor-fold>

    @Override
    public String toString() {
        return this.name;
    }
}
